package com.free.viewmover;

import com.free.app.MoveItemLayout;

import android.graphics.Rect;
import android.view.View;
import android.view.ViewGroup;

public class HorizontalAnimator extends Animator 
{	
	int mGabSize = 15;
	TranslateAniItem mTranslateMove = new TranslateAniItem();
	public void setSizeGab(int sizeGab)
	{		
		mGabSize = sizeGab;
	}
	
	public int getSizeGab()
	{
		return mGabSize;
	}	
	
	@Override
	protected void setUniformRect(ViewGroup root, Rect emptyRect, int l, int t, int r, int b)
	{
		int childLeft = root.getPaddingLeft();
		int childTop = root.getPaddingTop();
		int childBaseWidth = ( r - l ) / mVisibleViews.size();		
		int childBaseHeight = ( b - t ) / mVisibleViews.size();
		childBaseHeight -= emptyRect.top + emptyRect.bottom;
		childBaseWidth -= mGabSize * 2;
		childLeft += mGabSize + l;
		childTop += emptyRect.top;
		
		mUniformRect.set(childLeft, childTop, childLeft + childBaseWidth, childTop + childBaseHeight);		
	}
	
	@Override
	public void touchMoving(float x, float y)
	{
		mLastTouchDeltaX = x - mLastTouchX;		
		mTranslateMove.transDeltaX = mLastTouchDeltaX;			
		
		for (MoveItemLayout view : mVisibleViews)
		{
			if( view == null || view.getVisibility() == View.GONE)
				continue;
			
			FlipMover mover = view.getMover();
			mover.appalyAniItem(mTranslateMove);
			view.invalidate();
		}		
		
		for (MoveItemLayout view : mPreviousViews)
		{
			if( view == null || view.getVisibility() == View.GONE)
				continue;
			FlipMover mover = view.getMover();
			mover.appalyAniItem(mTranslateMove);
			view.invalidate();
		}
		
		for (MoveItemLayout view : mNextViews)
		{
			if( view == null || view.getVisibility() == View.GONE)
				continue;
			FlipMover mover = view.getMover();
			mover.appalyAniItem(mTranslateMove);
			view.invalidate();
		}
		
		mLastTouchX = x;
	}
	
	@Override
	public boolean endMove(float x, float y)
	{
		boolean changed = false;
		if( mLastTouchDeltaX > 29 )
		{
			changed = true;
		}
		return changed;
	}
}
